const { isCockroachDB } = require('./util/db-helpers');
const { expect } = require('chai');

// Out of the box, the CockroachDB docker container does not have the database that all
// the integration tests use. The code doesn't create it, so all the crdb tests fail.
// I've extracted this logic to a separate file so that it puts the requirements in
// place for both the "integration" and the "integration2" workflows
async function prepCRDB(knex) {
  const dbname = 'test';
  const configured = knex.client.connectionSettings.database;
  expect(configured).eq(dbname);
  if (configured === dbname) {
    await knex.raw(`DROP DATABASE IF EXISTS "${dbname}"`);
    await knex.raw(`CREATE DATABASE "${dbname}"`);
  }
}

async function prepDB(knex) {
  if (isCockroachDB(knex)) await prepCRDB(knex);
}

module.exports = { prepDB };
